﻿var express = require('express');
var router = express.Router();
var db = require('../dao/todoDao');
var dbCategory = require('../dao/categoryDao');
var dbTag = require('../dao/tagDao');
var site_type =	config = require("../config").site_type;
var date = require('../modules/date');
var dateUtils = require('../modules/dateutils');


exports.index = function(req, res, next){	
    var curDate = req.query.date || "";
    curDate = curDate==""?new Date():new Date(curDate);
    var curWeekIndex = dateUtils.GetWeekIndex(curDate);
    var weekStartDate = dateUtils.getBeginDateOfWeek1(curDate.getFullYear(), curWeekIndex);
    var weekEndDate = dateUtils.getEndDateOfWeek1(curDate.getFullYear(), curWeekIndex);

    var condition = {
        keyword: req.query.keyword || '',
        title: req.query.title || '',
        content: req.query.content || '',
        startDate: weekStartDate,
        endDate: weekEndDate,
        category: req.query.category || "",
        tag: req.query.tag || "",
        week: {index: curWeekIndex,
            rang: dateUtils.getWeekDateRang(curDate.getFullYear(), curWeekIndex)
        }
    };
	db.search(condition, function (err, todos) {
        if (err) {
            return next(err);
        }
        //console.log(todos);
		res.render('index.html', { 
			pageTitle: 'Express',
			controllerName: "todo",
			siteType: site_type,
			curType: site_type.todo,
			todos: todos,
            searchCondition: condition
		});
    });
};

exports.edit = function (req, res, next) {
    var id = req.params.id;

    dbCategory.findAll(function(err, categories){
        if (err) {
            return next(err);
        }
        //console.log(categories);
        if(categories==null || categories.length==0){                        
            dbCategory.add({title:"默认分类", is_edit:false, order:99}, function(err, categoryEntity){
                categories=[categoryEntity];
            });
        }
        dbTag.findAll(function(err, tags){
            if (err) {
                return next(err);
            }
            if(tags==null)  tags=[];
            if(id == 0){
                res.render('edit.html', {
                    siteType: site_type,
                    curType: site_type.todo,
                    todo: {_id:id, title:"", 
                        content:"", 
                        is_star:false, 
                        category_id: "0",
                        tag_ids:[],
                        todo_date: new Date()
                    },
                    tags: tags,
                    categories: categories
                });
            }
            else{
                db.findById(id, function (err, doc) {
                    if (err) {
                        return next(err);
                    }
                    console.log(doc)
                    res.render('edit.html', {
                        siteType: site_type,
                        curType: site_type.todo,
                        todo: doc,
                        tags: tags,
                        categories: categories
                    });
                });
            }

        });
    });
};

exports.editSubmit = function (req, res, next) {
    var today = new Date();
    var data = {
        _id: req.body.id || 0,
        title: req.body.title || '',
        content: req.body.content || '',
        isStar:  req.body.isStar || false,
        categoryId: req.body.categoryId || '',
        tagIds: req.body.tagIds || [],
        todoDate: (new Date(req.body.todoDate)).pattern('yyyy-MM-dd hh:mm') || today
    };

	data.title = data.title.trim();
    if (!data.title) {
        res.json({status:'0', msg: '标题是必须的'});
        //return res.render('error.html', {message: '标题是必须的'});
    }

    if(data._id == 0){
        db.add(data, function (err, row) {
            if (err) {
                return next(err);
            }
            res.json({status:'1'});
        });
    }
    else{
        db.edit(data, function (err, row) {
            if (err) {
                return next(err);
            }
            res.json({status:'1'});
        });
    }
    
};

exports.remove = function (req, res, next) {
    var id = req.params.id;

    db.remove(id, function (err, doc) {
        if (err) {
            return next(err);
        }    

		res.json({status:'1'});
    });
};

exports.doneSubmit = function (req, res, next) {
    var id = req.params.id;
    var done = req.body.done;

    db.doneTodo(id, done, function (err, doc) {
        if (err) {
            return next(err);
        }
		res.json({status:'1'});
    });
};

exports.starSubmit = function (req, res, next) {
    var id = req.params.id;
    var isStar = req.body.isStar;

    db.starTodo(id, isStar, function (err, doc) {
        if (err) {
            return next(err);
        }
        res.json({status:'1'});
    });
};

